package reflect;/*

 */

import com.sun.org.apache.bcel.internal.generic.NEW;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

/**
 * @ClassName reflectDome
 * @Author Final
 * 反射
 * @Project DataStructures
 */
public class ReflectDome {

    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {

        //创建对象方式方式一
//        UserEntity userEntity = new UserEntity();
        //创建对象方式方式二
//        Class<UserEntity> userEntityClass = UserEntity.class;
//        UserEntity userEntity1 = userEntityClass.newInstance();  //创建无参构造方法
//        System.out.println(userEntity1);

        //创建对象方式方式三 完整路径 报名+类名
//        Class<?> aClass = Class.forName("reflect.UserEntity");
//        Field name = aClass.getDeclaredField("name");
//        UserEntity o = (UserEntity) aClass.newInstance();
//        name.setAccessible(true);
//        name.set(o,"zhangsan");
//        System.out.println(o.getName());

//        Field name1 = aClass.getDeclaredField("name");
//        name1.set(o,"zhangshan");
//        Field name = aClass.getDeclaredField("userName");
//        name.set(o,"zhangsan");
//        System.out.println(o.getUserName());
//        Method make = aClass.getDeclaredMethod("make");
//        Object invoke1 = make.invoke(o);
//        System.out.println(invoke1);
//        Method sum = aClass.getDeclaredMethod("sum", Integer.class, Integer.class);//获取有参方法
//        sum.setAccessible(true);//设置权限，可以访问私有方法
//        Integer invoke = (Integer) sum.invoke(o, 1, 2);
//        System.out.println(invoke);
//        Field[] declaredFields = aClass.getDeclaredFields();
//        for (Field declaredField : declaredFields) {
//            System.out.println(declaredField);
//        }

//        Constructor<UserEntity> declaredConstructor = userEntityClass.getDeclaredConstructor(String.class, Integer.class);//创建有参构造方法
//        UserEntity xiaoming = declaredConstructor.newInstance("小明", 21);
//        System.out.println(xiaoming);
//
//        System.out.println(userEntity1);
//
//        System.out.println(userEntityClass.getName());
//        System.out.println(aClass.getClassLoader());
//        System.out.println( userEntityClass==aClass);

        ArrayList<String> arrayList=new ArrayList<>();
        arrayList.add("san");

        Class<? extends ArrayList> aClass = arrayList.getClass();
        Method addMethod = aClass.getDeclaredMethod("add", Object.class);
        addMethod.invoke(arrayList,1);

        System.out.println(arrayList);

    }
}
